package com.lottery.ruok.lottrey.activity;

import android.Manifest;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ListView;

import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.adapter.MyViewPagerAdapter;
import com.lottery.ruok.lottrey.adapter.QQAdapter;
import com.lottery.ruok.lottrey.adapter.WeChatAdapter;
import com.lottery.ruok.lottrey.retrofitModule.RetrofitBase;
import com.lottery.ruok.lottrey.retrofitModule.RxHelper;
import com.lottery.ruok.lottrey.retrofitModule.RxSubscribe;
import com.lottery.ruok.lottrey.retrofitModule.request.GetServiceRequest;
import com.lottery.ruok.lottrey.retrofitModule.response.GetServiceResponse;
import com.lottery.ruok.lottrey.ui.ShapeIndicatorView;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import kr.co.namee.permissiongen.PermissionFail;
import kr.co.namee.permissiongen.PermissionGen;
import kr.co.namee.permissiongen.PermissionSuccess;
import utils.Utils;

/**
 * Created by ASUS on 2018/4/27.
 * 联系客服
 */

public class CustomerServiceActivity extends BaseActivity {
    @BindView(R.id.custom_indicator)
    ShapeIndicatorView customIndicator;
    @BindView(R.id.tabs)
    TabLayout tabs;
    @BindView(R.id.vp_view)
    ViewPager vpView;
    private String imgUrl;//二维码地址

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cutomer_service);
        ButterKnife.bind(this);

        init();
    }

    private void init() {
        RetrofitBase.getRetrofit().create(GetServiceRequest.class)
                .getResult()
                .compose(RxHelper.<GetServiceResponse>handleResult())
                .subscribe(new RxSubscribe<GetServiceResponse>(this) {
                    @Override
                    protected void _onNext(final GetServiceResponse getServiceResponse) {
                        if(getServiceResponse != null){
                            String[] titles = {"QQ客服", "微信客服"};
                            ArrayList<View> viewArrayList = new ArrayList<>();
                            ListView listView1 = new ListView(CustomerServiceActivity.this);
                            ListView listView2 = new ListView(CustomerServiceActivity.this);
                            listView1.setAdapter(new QQAdapter(getServiceResponse.getQq(), CustomerServiceActivity.this));
                            listView1.setDividerHeight(Utils.dip2px(CustomerServiceActivity.this, 1));
                            listView1.setClipToPadding(false);
                            listView1.setPadding(0, Utils.dip2px(CustomerServiceActivity.this, 10), 0, 0);
                            WeChatAdapter weChatAdapter = new WeChatAdapter(getServiceResponse.getWechat(), CustomerServiceActivity.this);
                            weChatAdapter.setSaveLocal(new WeChatAdapter.SaveLocal() {
                                @Override
                                public void saveLocal(int position) {
                                    imgUrl = getServiceResponse.getWechat().get(position).getImage();
                                    PermissionGen.with(CustomerServiceActivity.this)
                                            .addRequestCode(100)
                                            .permissions(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                                            .request();
                                }
                            });
                            listView2.setAdapter(weChatAdapter);
                            listView2.setDividerHeight(Utils.dip2px(CustomerServiceActivity.this, 10));
                            listView2.setClipToPadding(false);
                            listView2.setPadding(0, Utils.dip2px(CustomerServiceActivity.this, 10), 0, 0);
                            viewArrayList.add(listView1);
                            viewArrayList.add(listView2);
                            vpView.setAdapter(new MyViewPagerAdapter(viewArrayList, titles));
                            vpView.setOffscreenPageLimit(2);
                            tabs.setupWithViewPager(vpView);
                            customIndicator.setupWithTabLayout(tabs);
                            customIndicator.setupWithViewPager(vpView);
                        }
                    }

                    @Override
                    protected boolean showDialog() {
                        return true;
                    }
                });
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        PermissionGen.onRequestPermissionsResult(this, requestCode, permissions, grantResults);
    }

    @PermissionSuccess(requestCode = 100)
    public void doSomething() {
        if (imgUrl != null) {
            Utils.showToast("二维码已经存储到本地");

            Utils.saveImageLocal(this, imgUrl);
        }
    }

    @PermissionFail(requestCode = 100)
    public void doFailSomething() {
        Utils.showToast("请打开SD卡权限以便能存储微信二维码");
    }
}